<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>设置推广群的推广位</title>
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  @include('public.style')
  <style>
    .layui-table-cell {
        height: 28px;
        line-height: 28px;
        padding: 0 15px;
        position: relative;
        box-sizing: border-box;
    }
  </style>
</head>
<body>


 @include('public.navbar')

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>设置推广群的推广位</legend>
</fieldset>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="AllHold">批量保存</button>
  </div>
</script>

<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="hold">保存</a>
</script>

	@include('public.script')

<script>
layui.use('table', function(){
  var table = layui.table;

  table.render({
    elem: '#test'
    ,url:'/user/get_friends?status=pi'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,title: '用户数据表'
    ,limit: 100
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'nickname', title:'群名', width:250}
      ,{field:'PositionId', title:'推广位ID', width:150, edit: 'text', sort: true},
	  ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:60}
    ]]

  });

  //头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
	  case 'AllHold'://批量保存
	    if(checkStatus.isAll){
			// layer.msg('全选');
			// layer.alert(JSON.stringify(checkStatus.data));
			$.ajax({
			            async : false,
			            type: "post",
			            url: "/user/fconfig?status=pc",
			            contentType:'application/json;charset=UTF-8',
			            data: JSON.stringify(checkStatus.data),
                        headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
			            error : function(request) {
								  layer.msg("error");
			            },
			            success : function(data) {
			                // alert(data.code);
								  if(data.code == 0){
									  layer.msg("批量设置推广位成功")
								  }else{
									  layer.msg("error:"+data.msg)
								  }
			            }
			        });
		}else{
			layer.msg('请先全选数据');
		}

	  break;

    };
  });

  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    //console.log(obj)
    if(obj.event === 'hold'){//单个保存
		// layer.msg(data.nickname);
		// console.log(data);
		var arr = new Array();
		arr[0]=data;
		console.log(JSON.stringify(arr));//[{"wxid":"21580117114@chatroom","nickname":"Teest","UnionId":null,"PositionId":123456,"status":2}]
		$.ajax({
		            async : false,
		            type: "post",
		            url: "/user/fconfig?status=pc",
		            contentType:'application/json;charset=UTF-8',
		            data: JSON.stringify(arr),
                    headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
		            error : function(request) {
							  layer.msg("error");
		            },
		            success : function(data) {
		                // alert(data.code);
							  if(data.code == 0){
								  layer.msg("设置推广位成功")
							  }else{
								  layer.msg("error:"+data.msg)
							  }
		            }
		        });
    }
  });
});
</script>

</body>
</html>
